import time
import urllib.parse
from requests.exceptions import RequestException
import requests
import json
import random


def get_page_index(offset, keyword):

    # 构造时间戳
    stamp = str(time.time())
    tiemarray = stamp.split('.')
    timestamp = int(tiemarray[0]+tiemarray[1][:3])

    data = {
        'aid': 24,
        'app_name': 'web_search',
        'offset': offset,
        'format': 'json',
        'keyword': keyword,
        'autoload': 'true',
        'count': 20,
        'en_qc': 1,
        'cur_tab': 1,
        'from': 'search_tab',
        'pd': 'synthesis',
        'timestamp': timestamp
    }
    url='https://www.toutiao.com/api/search/content/?'+urllib.parse.urlencode(data)

    headers = {
        'authority': 'www.toutiao.com',
        'accept': ' application/json, text/javascript',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN, zh',
        'content-type': 'application/x-www-form-urlencoded',
        'csrftoken' : '39a6c2d1fdafc6e5fe8dc7d99917526e',
        'referer': 'https://www.toutiao.com/search/?keyword=%E7%BD%91%E8%AF%BE',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4015.0 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest'
    }  # 构造成一个随机完整的User-Agent

    response = requests.get(url,headers=headers)
    try:
        if response.status_code == 200:
            return response.json()
    except RequestException:
        print('请求索引页面出错')


def main():
    data = get_page_index(0, '街拍')
    print(data)


if __name__ == "__main__":
    main()
